//
//  CWCSaveViewController.m
//  ChuanNews
//
//  Created by dllo on 16/4/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCSaveViewController.h"
#import "BaseHeader.h"
#import "CWCDataModel.h"
#import "CWCDetailReadingViewController.h"
@interface CWCSaveViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *saveCountArr;
@end

@implementation CWCSaveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的收藏";
    self.navigationController.navigationBar.translucent = NO;
    self.saveCountArr = [[DatabaseTool shareDatabaseTool]selectAllreading];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView jxl_setDayMode:^(UIView *view) {
        
        // 设置日间模式状态
        view.backgroundColor = [UIColor whiteColor]; // view为当前设置的视图
        
    } nightMode:^(UIView *view) {
        
        // 设置夜间模式状态
        view.backgroundColor = [UIColor blackColor]; // view为当前设置的视图
        
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.saveCountArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *result = @"result";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:result];
    if (!cell) {
        cell = [[CWCBaseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:result];
    }
    CWCDataModel *read = self.saveCountArr[indexPath.row];
    cell.textLabel.text = read.title;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCDataModel *read = self.saveCountArr[indexPath.row];
    CWCDetailReadingViewController * vc =[[CWCDetailReadingViewController alloc]init];
    vc.reading = read;
    vc.id = read.docid;
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCDataModel *read = self.saveCountArr[indexPath.row];
    int result = [[DatabaseTool shareDatabaseTool]deleteReading:read];
    if (result == 0) {
        [self.saveCountArr removeObjectAtIndex:indexPath.row];
        [self.tableView reloadData];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"删除失败" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"  style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:okAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}








-(void)dealloc{
    [_tableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
